package com.kang.until;

import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.kang.pojo.InventoryLiquidation;
import com.kang.pojo.InventoryLiquidation_itemInformation;
import com.kang.pojo.InventoryLoss;
import com.kang.pojo.InventoryLoss_itemInformation;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

/**
 * 生成PDF文件工具类
 * 
 * @author wd
 *
 */
public class GeneratingPDF {

	private static BaseFont bfChinese;
	private static com.lowagie.text.Font fontChinese_14;
	private static com.lowagie.text.Font fontChinese_16;
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 创建表格统一单元格样式
	 * @param str
	 * @return
	 */
	public static Cell createCell(String str,com.lowagie.text.Font font){
		Cell cell = null;
		try {
			cell = new Cell(new Phrase(str,font));
			cell.setBorderColor(new Color(0, 0, 0));
			 //单元格水平对齐方式
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        //单元格垂直对齐方式
	        cell.setVerticalAlignment(Element.ALIGN_CENTER);
		} catch (BadElementException e) {
			e.printStackTrace();
		}
		return cell;
	}
	
	//======================================库存报损========================================================
	/**
	 * 创建库存报损单头部信息
	 * @return
	 * @throws BadElementException 
	 */
	public static Table createInventoryLossTable(InventoryLoss loss) throws BadElementException{
		Table table = new Table(6);
		table.setBorderWidth(0);
		table.setBorderColor(new Color(0, 0, 0));
		table.setPadding(1);
		table.setSpacing(0);
		table.setWidth(100);
		
		List<Cell> cellStyleList = new ArrayList<Cell>();
		
		//-----------------------第一行-----------------------------
		Cell cell = new Cell(new Phrase("报损单号：",fontChinese_14));
		cellStyleList.add(cell);
		cell = new Cell(new Phrase(loss.getLossnumber(),fontChinese_14));
		cellStyleList.add(cell);
		
		cell = new Cell(new Phrase("申请人：",fontChinese_14));
		cellStyleList.add(cell);
		cell = new Cell(new Phrase(loss.getCreatepersonName(),fontChinese_14));
		cellStyleList.add(cell);
		
		cell = new Cell(new Phrase("提交人：",fontChinese_14));
		cellStyleList.add(cell);
		cell = new Cell(new Phrase(loss.getSubmitterName(),fontChinese_14));
		cellStyleList.add(cell);
		
		if(cellStyleList != null){
			int listSize = cellStyleList.size();
			for (int i = 0; i < listSize; i++) {
				cellStyleList.get(i).setBorder(0);
				cellStyleList.get(i).setBorderColor(new Color(0, 0, 0));
				//单元格水平对齐方式
				if(i==0 || i==2 || i==4){
					cellStyleList.get(i).setHorizontalAlignment(Element.ALIGN_RIGHT);
				}else{
					cellStyleList.get(i).setHorizontalAlignment(Element.ALIGN_LEFT);
				}
			    //单元格垂直对齐方式
				cellStyleList.get(i).setVerticalAlignment(Element.ALIGN_CENTER);
				table.addCell(cellStyleList.get(i));
			}
		}
		cellStyleList.clear();//清除集合
		
		//-----------------------第二行-----------------------------
		cell = new Cell(new Phrase("仓库名称：",fontChinese_14));
		cellStyleList.add(cell);
		cell = new Cell(new Phrase(loss.getWarehouseName(),fontChinese_14));
		cellStyleList.add(cell);
		
		cell = new Cell(new Phrase("报损说明：",fontChinese_14));
		cellStyleList.add(cell);
		cell = new Cell(new Phrase(loss.getLossdescription(),fontChinese_14));
		cell.setColspan(3);//跨列
		cellStyleList.add(cell);
		
		if(cellStyleList != null){
			int listSize = cellStyleList.size();
			for (int i = 0; i < listSize; i++) {
				cellStyleList.get(i).setBorder(0);
				cellStyleList.get(i).setBorderColor(new Color(0, 0, 0));
				//单元格水平对齐方式
				if(i==0 || i==2){
					cellStyleList.get(i).setHorizontalAlignment(Element.ALIGN_RIGHT);
				}else{
					cellStyleList.get(i).setHorizontalAlignment(Element.ALIGN_LEFT);
				}
			    //单元格垂直对齐方式
				cellStyleList.get(i).setVerticalAlignment(Element.ALIGN_CENTER);
				table.addCell(cellStyleList.get(i));
			}
		}
		
		return table;
		
	}
	
	/**
	 * 创建库存报损物品列表导出表格
	 * @param itemList	报损物品集合
	 * @return
	 * @throws BadElementException
	 */
	public static Table createInventoryLossItemListTable(List<InventoryLoss_itemInformation> itemList) throws BadElementException{
		Table table = new Table(8);
		table.setBorderWidth(1);
		table.setBorderColor(new Color(0, 0, 0));
		table.setPadding(1);
		table.setSpacing(0);
		table.setWidth(100);
		//表头说明
		Cell cell = createCell("报损物品信息列表",fontChinese_14);
		cell.setHeader(true);
		cell.setColspan(8);
		table.addCell(cell);
		//cell.setRowspan(2);跨行
		//cell.setColspan(2);跨列
		//表头
		String[] tableHeader = new String[]{"序号","物品批次","物品名称","物品条码","品牌","类别","小计","报损说明"};
		List<Cell> tableHeaderCellList = new ArrayList<Cell>();
		for (int i = 0; i < tableHeader.length; i++) {
			cell = createCell(tableHeader[i],fontChinese_14);
			tableHeaderCellList.add(cell);
		}
		for (int i = 0; i < tableHeader.length; i++) {
			table.addCell(tableHeaderCellList.get(i));
		}
		// 添加此代码后超出每页都会显示表头
		table.endHeaders();
		
		//表格
		if(itemList!=null){
			int listSize=itemList.size();
			for (int i = 0; i < listSize; i++) {
				Cell cell1 = createCell(i+1+"",fontChinese_14);
				Cell cell2 = createCell(itemList.get(i).getBatchNumber(),fontChinese_14);
				Cell cell3 = createCell(itemList.get(i).getChineseName(),fontChinese_14);
				Cell cell4 = createCell(itemList.get(i).getArticleBarCode(),fontChinese_14);
				Cell cell5 = createCell(itemList.get(i).getBrand(),fontChinese_14);
				Cell cell6 = createCell(itemList.get(i).getCategoryName(),fontChinese_14);
				Cell cell7 = createCell(itemList.get(i).getLossNumber().toString()+" "
						+itemList.get(i).getSalesUnit()
						+"（"+itemList.get(i).getPurchaseSpecifications()
						+itemList.get(i).getSalesUnit()+"/"+itemList.get(i).getPurchasingUnit()+"）",fontChinese_14);
				Cell cell8 = createCell(itemList.get(i).getLossReason(),fontChinese_14);
				table.addCell(cell1);
				table.addCell(cell2);
				table.addCell(cell3);
				table.addCell(cell4);
				table.addCell(cell5);
				table.addCell(cell6);
				table.addCell(cell7);
				table.addCell(cell8);
			}
		}
		return table;
	}
	
	/**
	 * 生成库存报损PDF单
	 * 
	 * @return
	 * @throws Exception
	 */
	public static File generateInventoryLossPDF(InventoryLoss loss,List<InventoryLoss_itemInformation> itemList){

		try {
			bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		} catch (DocumentException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
		fontChinese_14 = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
		fontChinese_16 = new com.lowagie.text.Font(bfChinese, 16, com.lowagie.text.Font.NORMAL);
		
		// Rectangle pageSize = new Rectangle(270, 300);//设置文档宽高
		Document document = new Document(PageSize.A4,10, 10, 10, 10);// 分别为文档的大小、左、右、上、下的页边距
		//System.out.println("======================"+document.getPageSize().getWidth());
		
		File pdfFile = new File(FileUtil.TEMP_PATH +"库存报损单"+loss.getLossnumber()+ ".pdf");
		try {
			PdfWriter.getInstance(document,new FileOutputStream(pdfFile.getAbsolutePath()));
		} catch (FileNotFoundException | DocumentException e1) {
			e1.printStackTrace();
		}
		
		document.open();

		try {
			Paragraph title = new Paragraph("库存报损单", fontChinese_16);
			title.setAlignment(1);// 居中对齐
			document.add(title);
			document.add(createInventoryLossTable(loss));
			document.add(createInventoryLossItemListTable(itemList));
			document.add(new Paragraph(" ", fontChinese_14));
			document.add(new Paragraph("打印时间："+sdf.format(new Date()), fontChinese_14));
			document.add(new Paragraph(" ", fontChinese_14));
			document.add(new Paragraph("负责人签名：", fontChinese_14));
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		document.close();
		return pdfFile;
	}

	
	//======================================库存盘点========================================================
	/**
	 * 创建库存报损单头部信息
	 * @return
	 * @throws BadElementException 
	 */
	public static Table createInventoryLiquidationTable(InventoryLiquidation pandian) throws BadElementException{
		Table table = new Table(6);
		table.setBorderWidth(0);
		table.setBorderColor(new Color(0, 0, 0));
		table.setPadding(1);
		table.setSpacing(0);
		table.setWidth(100);
		
		List<Cell> cellStyleList = new ArrayList<Cell>();
		
		//-----------------------第一行-----------------------------
		Cell cell = new Cell(new Phrase("盘点单号：",fontChinese_14));
		cellStyleList.add(cell);
		cell = new Cell(new Phrase(pandian.getInventoryNumber(),fontChinese_14));
		cellStyleList.add(cell);
		
		cell = new Cell(new Phrase("申请人：",fontChinese_14));
		cellStyleList.add(cell);
		cell = new Cell(new Phrase(pandian.getCreatePersonName(),fontChinese_14));
		cellStyleList.add(cell);
		
		cell = new Cell(new Phrase("提交人：",fontChinese_14));
		cellStyleList.add(cell);
		cell = new Cell(new Phrase(pandian.getSubmitterName(),fontChinese_14));
		cellStyleList.add(cell);
		
		if(cellStyleList != null){
			int listSize = cellStyleList.size();
			for (int i = 0; i < listSize; i++) {
				cellStyleList.get(i).setBorder(0);
				cellStyleList.get(i).setBorderColor(new Color(0, 0, 0));
				//单元格水平对齐方式
				if(i==0 || i==2 || i==4){
					cellStyleList.get(i).setHorizontalAlignment(Element.ALIGN_RIGHT);
				}else{
					cellStyleList.get(i).setHorizontalAlignment(Element.ALIGN_LEFT);
				}
			    //单元格垂直对齐方式
				cellStyleList.get(i).setVerticalAlignment(Element.ALIGN_CENTER);
				table.addCell(cellStyleList.get(i));
			}
		}
		cellStyleList.clear();//清除集合
		
		//-----------------------第二行-----------------------------
		cell = new Cell(new Phrase("仓库名称：",fontChinese_14));
		cellStyleList.add(cell);
		cell = new Cell(new Phrase(pandian.getWarehouseName(),fontChinese_14));
		cellStyleList.add(cell);
		
		cell = new Cell(new Phrase("盘点说明：",fontChinese_14));
		cellStyleList.add(cell);
		cell = new Cell(new Phrase(pandian.getInventoryDescription(),fontChinese_14));
		cell.setColspan(3);//跨列
		cellStyleList.add(cell);
		
		if(cellStyleList != null){
			int listSize = cellStyleList.size();
			for (int i = 0; i < listSize; i++) {
				cellStyleList.get(i).setBorder(0);
				cellStyleList.get(i).setBorderColor(new Color(0, 0, 0));
				//单元格水平对齐方式
				if(i==0 || i==2){
					cellStyleList.get(i).setHorizontalAlignment(Element.ALIGN_RIGHT);
				}else{
					cellStyleList.get(i).setHorizontalAlignment(Element.ALIGN_LEFT);
				}
			    //单元格垂直对齐方式
				cellStyleList.get(i).setVerticalAlignment(Element.ALIGN_CENTER);
				table.addCell(cellStyleList.get(i));
			}
		}
		
		return table;
		
	}
	
	/**
	 * 创建库存报损物品列表导出表格
	 * @param itemList	报损物品集合
	 * @return	表格
	 * @throws BadElementException
	 */
	public static Table createInventoryLiquidationItemListTable(List<InventoryLiquidation_itemInformation> itemList) throws BadElementException{
		Table table = new Table(8);
		table.setBorderWidth(1);
		table.setBorderColor(new Color(0, 0, 0));
		table.setPadding(1);
		table.setSpacing(0);
		table.setWidth(100);
		//表头说明
		Cell cell = createCell("盘点物品信息列表",fontChinese_14);
		cell.setHeader(true);
		cell.setColspan(8);
		table.addCell(cell);
		//cell.setRowspan(2);跨行
		//cell.setColspan(2);跨列
		//表头
		String[] tableHeader = new String[]{"序号","物品批次","物品名称","物品条码","品牌","盘点前数量","盘点后小计","盈亏数量"};
		List<Cell> tableHeaderCellList = new ArrayList<Cell>();
		for (int i = 0; i < tableHeader.length; i++) {
			cell = createCell(tableHeader[i],fontChinese_14);
			tableHeaderCellList.add(cell);
		}
		for (int i = 0; i < tableHeader.length; i++) {
			table.addCell(tableHeaderCellList.get(i));
		}
		// 添加此代码后超出每页都会显示表头
		table.endHeaders();
		
		//表格
		if(itemList!=null){
			int listSize=itemList.size();
			for (int i = 0; i < listSize; i++) {
				Cell cell1 = createCell(i+1+"",fontChinese_14);
				Cell cell2 = createCell(itemList.get(i).getBatchNumber(),fontChinese_14);
				Cell cell3 = createCell(itemList.get(i).getChineseName(),fontChinese_14);
				Cell cell4 = createCell(itemList.get(i).getArticleBarCode(),fontChinese_14);
				Cell cell5 = createCell(itemList.get(i).getBrand(),fontChinese_14);
				//Cell cell6 = createCell(itemList.get(i).getCategoryName(),fontChinese_14);
				Cell cell7 = createCell(itemList.get(i).getRecordNumber()==null?itemList.get(i).getNumber().toString()+" "+itemList.get(i).getSalesUnit():itemList.get(i).getRecordNumber().toString()+" "+itemList.get(i).getSalesUnit(),fontChinese_14);
				Cell cell8 = createCell(itemList.get(i).getInventoryInventoryNumber().toString()+" "
						+itemList.get(i).getSalesUnit()
						+"（"+itemList.get(i).getPurchaseSpecifications()
						+itemList.get(i).getSalesUnit()+"/"+itemList.get(i).getPurchasingUnit()+"）",fontChinese_14);
				Cell cell9 = createCell(itemList.get(i).getProfitLossNumber().toString()+" "+itemList.get(i).getSalesUnit(),fontChinese_14);
				table.addCell(cell1);
				table.addCell(cell2);
				table.addCell(cell3);
				table.addCell(cell4);
				table.addCell(cell5);
				//table.addCell(cell6);
				table.addCell(cell7);
				table.addCell(cell8);
				table.addCell(cell9);
			}
		}
		return table;
	}
	
	/**
	 * 生成库存盘点PDF单
	 * @param pandian	盘点信息（实体类）
	 * @param itemList	盘点的物品集合
	 * @return	pdf文件
	 */
	public static File generateInventoryLiquidationPDF(
			InventoryLiquidation pandian,
			List<InventoryLiquidation_itemInformation> itemList) {
		
		try {
			bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		} catch (DocumentException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
		fontChinese_14 = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
		fontChinese_16 = new com.lowagie.text.Font(bfChinese, 16, com.lowagie.text.Font.NORMAL);
		
		// Rectangle pageSize = new Rectangle(270, 300);//设置文档宽高
		Document document = new Document(PageSize.A4,10, 10, 10, 10);// 分别为文档的大小、左、右、上、下的页边距
		//System.out.println("======================"+document.getPageSize().getWidth());
		
		File pdfFile = new File(FileUtil.TEMP_PATH +"库存盘点单"+pandian.getInventoryNumber()+ ".pdf");
		try {
			PdfWriter.getInstance(document,new FileOutputStream(pdfFile.getAbsolutePath()));
		} catch (FileNotFoundException | DocumentException e1) {
			e1.printStackTrace();
		}
		
		document.open();

		try {
			Paragraph title = new Paragraph("库存盘点单", fontChinese_16);
			title.setAlignment(1);// 居中对齐
			document.add(title);
			document.add(createInventoryLiquidationTable(pandian));
			document.add(createInventoryLiquidationItemListTable(itemList));
			document.add(new Paragraph(" ", fontChinese_14));
			document.add(new Paragraph("打印时间："+sdf.format(new Date()), fontChinese_14));
			document.add(new Paragraph(" ", fontChinese_14));
			document.add(new Paragraph("负责人签名：", fontChinese_14));
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		document.close();
		return pdfFile;
	}
	
	
}
